package com.j.lemon.learn.clickhouse;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import com.alibaba.druid.pool.DruidDataSource;
import com.j.lemon.learn.utils.SequenceUtil;
import org.springframework.jdbc.core.JdbcTemplate;

import java.sql.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author zhangfeng
 */
public class CkDataInsert1 {
    private static final String DRIVER = "ru.yandex.clickhouse.ClickHouseDriver";
    private static final String URL = "jdbc:clickhouse://10.28.149.247:38123/default?serverTimezone=Asia/Shanghai";
    private static final String USER = "audit4a";
    private static final String PWD = "rryzggTR";
    private static final Integer COUNT = 100000000;
    private static final Integer THREAD_COUNT = 50000;
    private static final Integer THREAD_TASK_COUNT = 100;
    private static final Integer RANDOM_START = 100000000;
    private static final Integer RANDOM_END = 101000000;
    private static final Integer DP_RULE_ID = 11111;
    private static final List<String> COLUMN_LIST = Arrays.asList("main_acct_name", "main_acct_id", "person_org_id", "resource_id", "resource_group_id", "user_mail", "object_type", "object_id");
    private static final String START_TIME = "2020-08-01 00:00:00";
    private static final Integer DATA_SPACE = 20;
    private static final List<String> CONTENTS = Arrays.asList("utmpdump", "/var/log/wtmp", "history -c");
    private static AtomicInteger ic = new AtomicInteger(0);
    private static List<Integer> list = new ArrayList<>();

    private static final ExecutorService executorService = new ThreadPoolExecutor(210, 210, 0, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<>(10000), r -> new Thread(r, "bigData"));


    public static void main(String[] args) throws InterruptedException, SQLException {

        String driverClassName = "ru.yandex.clickhouse.ClickHouseDriver";
        String url = "jdbc:clickhouse://10.28.149.247:38123/audit";
        String userName = "audit4a";
        String password = "rryzggTR";
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName(driverClassName);
        dataSource.setUrl(url);
        dataSource.setUsername(userName);
        dataSource.setPassword(password);
        dataSource.setTestWhileIdle(true);
        dataSource.setMaxActive(100);
        dataSource.setMaxWait(60000);
        dataSource.setTestOnBorrow(false);

        Connection connection = dataSource.getConnection();



        String url2 = "jdbc:clickhouse://10.28.149.247:18123/audit";
        DruidDataSource dataSource2 = new DruidDataSource();
        dataSource2.setDriverClassName(driverClassName);
        dataSource2.setUrl(url2);
        dataSource2.setUsername(userName);
        dataSource2.setPassword(password);
        dataSource2.setTestWhileIdle(true);
        dataSource2.setMaxActive(100);
        dataSource2.setMaxWait(60000);
        dataSource2.setTestOnBorrow(false);

        JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource2);
        List<Long> userIds = jdbcTemplate.queryForList("select user_id from bd_imc_info_user_all limit 10000", Long.class);




        String sql = "INSERT INTO `ais_audit_log` (id, dp_rule_id,main_acct_name, main_acct_id, person_org_id, resource_id,\n" +
                "                             resource_group_id, client_port, server_mac, op_time, main_acct_status_id, main_acct_status,\n" +
                "                             person_duty_id, person_duty_name, acct_type,\n" +
                "                             appr_main_acct_name, appr_main_acct_id, appr_person_area_id, appr_person_area_name,\n" +
                "                             operate_content)\n" +
                "values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
        PreparedStatement ps = connection.prepareStatement(sql);


        DateTime parse = DateUtil.parse(START_TIME);
        long dpRuleId = 669362958005960704L;
        String mainAcctName = "xxx";
        long mainAcctId = 123321123L;
        long personOrgId = 123321123L;
        long resourceId = 123321123L;
        long resourceGroupId = 123321123L;
        String clientPort = "123";
        String serverMac = "123";
        String mainAcctStatusId = "123";
        String mainAcctStatus = "1";
        long personDutyId = 123321123L;
        String personDutyName = "aaaaaaaa";
        String apprMainAcctName = "aaaaaaaa";
        long apprMainAcctId = 123321123L;
        long apprPersonAreaId = 123321123L;
        String apprPersonAreaName = "aaaaaaaa";
        String acctType = "1";
        long objectId = 123321123L;
        long start = System.currentTimeMillis();
        for (int i = 0; i < 100000000; i++) {
            Long id = SequenceUtil.genLongSeqId();
            int index = RandomUtil.randomInt(1, 4444);
            long userId = userIds.get(index);
            DateTime dateTime = DateUtil.offsetSecond(parse, i + 1);
            String opTime = DateUtil.formatDateTime(dateTime);
            String operateContent = "";
            if (i % 20 == 0) {
                int randomContent = RandomUtil.randomInt(0, 3);
                String rdContent = CONTENTS.get(randomContent);
                operateContent = rdContent + "xxxxxxxxxx" + rdContent;
            } else {
                operateContent = "abcdefg";
            }
            ps.setLong(1, id);
            ps.setLong(2, dpRuleId);
            ps.setString(3, mainAcctName);
            ps.setLong(4, userId);
            ps.setLong(5, personOrgId);
            ps.setLong(6, resourceId);
            ps.setLong(7, resourceGroupId);
            ps.setString(8, clientPort);
            ps.setString(9, serverMac);
            ps.setString(10, opTime);
            ps.setString(11, mainAcctStatusId);
            ps.setString(12, mainAcctStatus);
//            ps.setLong(13, objectId);
            ps.setLong(13, personDutyId);
            ps.setString(14, personDutyName);
            ps.setString(15, acctType);
            ps.setString(16, apprMainAcctName);
            ps.setLong(17, apprMainAcctId);
            ps.setLong(18, apprPersonAreaId);
            ps.setString(19, apprPersonAreaName);
            ps.setString(20, operateContent);
            ps.addBatch();
            if (i % 10000 == 0) {
                ps.executeBatch();
                long end = System.currentTimeMillis();
                System.out.println("10000条数据插入完成,耗时：" + (end - start));
                start = end;
            }
        }
    }

}
